﻿using System;
using System.Linq;
using System.Web.UI;

namespace Carbon.Web.Forms.Controls.FileUpload
{
    public abstract class FileUploadEditorLayout : UserControl, IEditor, IDataProxy, IViewControl
    {
		#region Fields (6)
		private FileUploadEditor m_FileUploadEditor = default(FileUploadEditor);

		private FileReferenceCollection m_References = null;

		private bool m_MaxNumberOfFiles_calculated = false;
		private bool m_OnlyFileReference = default(bool);
		private bool m_OnlyFileReference_calculated = false;

		private int m_MaxNumberOfFiles = default(int);
		#endregion

		#region Properties (10)
		public FileUploadEditor FileUploadEditor
        {
            get
            {
                if (m_FileUploadEditor == null) throw new NullReferenceException("m_FileUploadEditor");
                return m_FileUploadEditor;
            }
            set
            {
                m_FileUploadEditor = value;
            }
        }
		public bool IsNull
        {
            get { return m_References == null || m_References.Count < 1; }
        }
		public int MaxNumberOfFiles
        {
            get
            {
                if (!m_MaxNumberOfFiles_calculated)
                {
                    m_MaxNumberOfFiles = OnlyFileReference ? 1 : FileUploadEditor.MaxNumberOfFiles;
                    m_MaxNumberOfFiles_calculated = true;
                }

                return m_MaxNumberOfFiles;
            }
        }
		public int NumberOfAdditionalFilesThatCanBeUploaded
        {
            get
            {
                if ((MaxNumberOfFiles - References.Count >= 0) == false) throw new InvalidOperationException("(MaxNumberOfFiles - References.Count >= 0) == false");
                int number = MaxNumberOfFiles - References.Count;
                if ((number >= 0) == false) throw new InvalidOperationException("(number >= 0) == false");
                return number;
            }
        }
		public bool OnlyFileReference
        {
            get
            {
                if (!m_OnlyFileReference_calculated)
                {
                    m_OnlyFileReference = FileUploadEditor.Binder.Property.PropertyType == typeof(FileReference);
                    m_OnlyFileReference_calculated = true;
                }

                return m_OnlyFileReference;
            }
        }
		public object DataContext
        {
            get { return References; }
        }
		public abstract String PopupUrl
        {
            get;
        }
		public FileReferenceCollection References
        {
            get
            {
                if (m_References == null)
                    m_References = new FileReferenceCollection();
                return m_References;
            }
        }
		public bool ShowAddButton
        {
            get
            {
                return NumberOfAdditionalFilesThatCanBeUploaded > 0;
            }
        }
		public object Value
        {
            get
            {
                if ((m_References.Count <= FileUploadEditor.MaxNumberOfFiles) == false) throw new InvalidOperationException("(m_References.Count <= FileUploadEditor.MaxNumberOfFiles) == false");

                if (OnlyFileReference)
                {
                    if (m_References == null || m_References.Count == 0)
                        return null;
                    else
                        return m_References.Single();
                }
                else
                {
                    if (m_References == null)
                        m_References = new FileReferenceCollection();

                    if ((m_References.Count <= MaxNumberOfFiles) == false) throw new InvalidOperationException("(m_References.Count <= MaxNumberOfUploadFiles) == false");
                    return m_References;
                }
            }
            set
            {
                if (OnlyFileReference)
                {
                    m_References = new FileReferenceCollection();
                    if (value != null)
                    {
                        m_References.Add((FileReference)value);
                    }
                }
                else
                {
                    m_References = (FileReferenceCollection)value;

                    if (m_References == null)
                        m_References = new FileReferenceCollection();
                }
            }
        }
		#endregion

		#region ASP Processing

		protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            EnsureChildControls();
        }

		#endregion

        protected override void LoadViewState(object savedState)
        {
            ViewControlUpdateState.OnLoadViewState(this, savedState, base.LoadViewState);
        }

        public virtual void OnUpdateControl(object sender, UpdateControlEventArgs args)
        {
        }

        public virtual void OnUpdateModel(object sender, UpdateModelEventArgs args)
        {
        }

        protected override void OnPreRender(EventArgs e)
        {
            ViewControlUpdateState.OnPreRender(this, e, base.OnPreRender);
        }

        protected override object SaveViewState()
        {
            return ViewControlUpdateState.OnSaveViewState(base.SaveViewState);
        }

        private ViewControlUpdateState m_PipelineState = default(ViewControlUpdateState);
        public ViewControlUpdateState PipelineState
        {
            get
            {
                return m_PipelineState;
            }
            set
            {
                m_PipelineState = value;
            }
        } 
    }
}
